#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  opt31.py
#       Author @  Huoty
#  Create date @  2016-06-30 17:39:42
#  Description @
# *************************************************************

import os
import argparse
from pyquery import PyQuery as pq

def extract_url_from_web(web_url):
    doc = pq(url=web_url)
    urls = []
    for a in doc('a'):
        a = pq(a)
        href = a.attr("href")
        if href:
            urls.append(href)

    return urls

def add_query_for_url(url, query):
    query = query.replace(',', '&')
    return url.replace('?', '?' + query + '&') if '?' in url else url + '?' + query

def main(url, query, outfile):
    with open(os.path.abspath(outfile), 'w') as f:
        for item in extract_url_from_web(url):
            newurl = add_query_for_url(item, query)
            f.write(newurl+'\n')
        pass

# Script starts from here

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-u", dest="url", type=str)
    parser.add_argument("-d", dest="query", type=str)
    parser.add_argument("-o", dest="outfile", type=str)

    options = parser.parse_args()
    main(**vars(options))
